package cn.com.manager.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.binary.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.manager.dao.PostInfoMapper;
import cn.com.manager.dao.weibo.PostDao;
import cn.com.manager.dao.weibo.UserDao;
import cn.com.manager.pojo.PostInfo;
import cn.com.manager.service.WeiboService;

@Service
public class WeiboServiceImpl implements WeiboService {
	@Autowired
	private UserDao userDao;
	@Autowired
	private PostDao postDao;
	@Autowired
	private PostInfoMapper postInfoMapper;

	@Override
	public void saveUser(String username, String password) {
		userDao.saveUser(username, password);
	}

	@Override
	public List<String> newRegisterUser() {
		return userDao.newUserList();
	}

	@Override
	public String userLogin(String username, String password) {
		Map<String, Object> userInfo = userDao.getUserInfo(username);
		if (StringUtils.equals(username, String.valueOf(userInfo.get("username")))
				&& StringUtils.equals(password, String.valueOf(userInfo.get("password")))) {
			return String.valueOf(userInfo.get("userId"));
		}
		return null;
	}

	@Override
	public Map<String, Object> getUserInfo(String username) {
		Map<String, Object> userInfo = userDao.getUserInfo(username);
		userInfo.remove("password");
		return userInfo;
	}

	@Override
	public void follow(String uid, String currentUserId) {
		// 添加关注
		userDao.followUser(uid, currentUserId);
		// 添加被关注
		userDao.follower(uid, currentUserId);
	}

	@Override
	public void cancelFollow(String uid, String currentUserId) {
		// 取消关注
		userDao.cancelFollow(uid, currentUserId);
		// 取消被关注
		userDao.cancelFollower(uid, currentUserId);
	}

	@Override
	public boolean isFollow(String username, String currentUserId) {
		return userDao.isFollow(username, currentUserId);
	}

	@Override
	public Map<String, Object> getUserInfoById(String userid) {
		Map<String, Object> userInfo = userDao.getUserInfoById(userid);
		userInfo.remove("password");
		return userInfo;
	}

	@Override
	public Map<String, Long> followInfoAmount(String currentUserId) {
		Map<String, Long> followInfo = new HashMap<String, Long>();
		// 获取关注数量
		followInfo.put("followingAmount", userDao.followingAmount(currentUserId));
		// 获取粉丝数量
		followInfo.put("followerAmount", userDao.followerAmount(currentUserId));
		return followInfo;
	}

	@Override
	public void publish(String userid, String content) {
		Map<String, Object> userInfo = userDao.getUserInfoById(userid);
		String postid = postDao.postSave(userid, String.valueOf(userInfo.get("username")),
				String.valueOf(System.currentTimeMillis()), content);
		postDao.saveOldPost(userid, postid);
		postDao.recentPostSave(userid, postid);
		postDao.saveRecentPost(postid);
	}

	@Override
	public List<Map<String, Object>> getPost(String userid) {
		// 查询当前用户的关注人的id
		List<String> followingUserId = userDao.followingUserId(userid);
		followingUserId.add(userid);
		// 获取上次获取最大微博的id
		Double lastPostIdByUserId = postDao.getLastPostIdByUserId(userid);
		if (lastPostIdByUserId == null) {
			lastPostIdByUserId = 0d;
		} else {
			lastPostIdByUserId = lastPostIdByUserId + 1;
		}
		List<String> postIds = new ArrayList<String>();
		// 获取微博信息
		for (String uid : followingUserId) {
			List<String> postIdsByUserId = postDao.getPostIdByUserId(uid, lastPostIdByUserId);
			if (postIdsByUserId != null && !postIdsByUserId.isEmpty()) {
				postIds.addAll(postIdsByUserId);
			}
		}
		if (postIds != null && !postIds.isEmpty()) {
			// 排序
			Collections.sort(postIds);
			Collections.reverse(postIds);
			// 保存上次获取最大微博id
			postDao.setLastPostIdByUserId(userid, postIds.get(0));
			// 将微博id加入到收到列表
			postIds.forEach(bean -> postDao.saveReceivePostId(userid, bean));
		}
		return postDao.getReceivePost(userid);

	}

	@Override
	public List<Map<String, Object>> getPost() {
		return postDao.getRecentPost();
	}

	@Override
	public void ColdDataProcessing() {
		try{
			int n = 0;
			while (n++ < 1000) {
				Map<String, Object> postInfo = postDao.getoldPost();
				if (postInfo == null)
					break;
				PostInfo info = new PostInfo();
				info.setPostId(Integer.parseInt((String) postInfo.get("postId")));
				info.setUserId(Integer.valueOf((String) postInfo.get("userId")));
				info.setUsername((String) postInfo.get("username"));
				info.setContent((String) postInfo.get("content"));
				info.setCreateTime(new Date((Long) postInfo.get("time")));
				postInfoMapper.insert(info);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}
